package com.example.spring.bean.load07factory;

import com.example.spring.pojo.User;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * 测试类
 * <p>
 * <p>
 */
public class FactoryTest {

    public static void main(String[] args) throws Exception {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(InitConfig.class);


        //AbstractBeanFactory.getBean ->
        //AbstractBeanFactory.doGetBean ->
        //AbstractBeanFactory.getObjectForBeanInstance->
        //AbstractBeanFactory.getObjectFromFactoryBean->
        // FactoryBeanRegistrySupport.getObjectFromFactoryBean->
        // FactoryBeanRegistrySupport.doGetObjectFromFactoryBean内 factory.getObject()
        Object bean = context.getBean("customFactoryBean");

        //bean的类型:   class com.example.spring.vo.spring.User
        //因为实现FactoryBean的getObjectType()方法，类型已经改变
        //所以context.getBean()时，获取的对象是调用的CustomFactoryBean.getObject()获得对象
        System.out.println("bean的类型:" + bean.getClass());

        Object bean2 = context.getBean("&customFactoryBean");
        //bean2: class com.spring.base.factory.CustomFactoryBean
        System.out.println("bean2===" + bean2.getClass());


        //转换没有报错,说明User已经注入到容器中，但beanId是customFactoryBean
        User bean1 = (User) bean;
        System.out.println(bean1.getId());


        System.out.println("-------------------");
        //输出容器中的bean
        String[] names = context.getBeanDefinitionNames();
        for (String name : names) {
            //beanId 是 customFactoryBean
            System.out.println(name);
        }
    }
}
